package com.nutricia.biz.convertor;

import com.nutricia.common.api.info.MenuAndPermissionInfo;
import com.nutricia.core.model.domain.MenuAndPermissionDO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 权限联合菜单转换器
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
public class MenuUnionPermissionConvertor {


    /**
     * 将 MenuAndPermission 转化为 MenuAndPermissionDo
     *
     * 递归获取下级菜单数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午6:13
     */
    public static MenuAndPermissionInfo convertToMenuAndPermissionInfo(MenuAndPermissionDO menuAndPermissionDO){
        if(null == menuAndPermissionDO){
            return null;
        }
        MenuAndPermissionInfo menuAndPermissionInfo = new MenuAndPermissionInfo();
        menuAndPermissionInfo.setMenuId(menuAndPermissionDO.getMenuId());
        menuAndPermissionInfo.setMenuName(menuAndPermissionDO.getMenuName());
        menuAndPermissionInfo.setSystemPermissionInfos(
                SystemPermissionConvertor.convertToSystemPermissionInfoss(menuAndPermissionDO.getSystemPermissionDOS()));
        menuAndPermissionInfo.setSubMenuInfos(MenuUnionPermissionConvertor.convertToMenuAndPermissionDoS(menuAndPermissionDO.getSubMenus()));
        return menuAndPermissionInfo;
    }

    /**
     * 将 MenuAndPermissions 转化为 MenuAndPermissionDos
     *
     */
    public static List<MenuAndPermissionInfo> convertToMenuAndPermissionDoS(List<MenuAndPermissionDO> menuAndPermissionDOS){
        if(CollectionUtils.isNotEmpty(menuAndPermissionDOS)){
            List<MenuAndPermissionInfo> menuAndPermissionInfos = new ArrayList<>(menuAndPermissionDOS.size());
            menuAndPermissionDOS.forEach(menuAndPermissionDO -> {
                menuAndPermissionInfos.add(MenuUnionPermissionConvertor.convertToMenuAndPermissionInfo(menuAndPermissionDO));
            });
            return menuAndPermissionInfos;
        }
        return Collections.emptyList();
    }

}
